<?php
defined ( 'IN_SYS' ) or die ( 'Restricted Access' );
class SysException extends Exception {
	protected $cause;
	protected $status='';
	
	function __construct ($p1=NULL, $status=NULL) {
		$this->status = $status;
		$cause = NULL;
		
		if ($p1 instanceof Exception) {
			$msg = "";
			$cause = $p1;
		} else {
			$msg = $p1;
		}
		
		parent::__construct ( $msg );
		
		if ($cause !== NULL) {
			$this->cause = $cause;
			$this->message .= " [wrapped: " . $this->getMessage () . "]";
		}
	}
	
	/**
	 * Get Cause
	 *
	 */
	public function getCause() {
		return $this->cause;
	}
	
	function printError(){
		
		if (IS_DEBUG) {
			if (is_null($this->status)) {
				$status = EXCEPTION_ERROR_SQL;
			} else {
				$status = $this->status;
			}
			Page::setHeader($status);
			$html = '<div class="system_error"><pre>';
			$html.= '<div class="error_title">Error('.$status.'): '.get_class($this) . " '{$this->message}' <br/>in <b>{$this->file}({$this->line})</b></div>";
			$html.= '<ul>';
			foreach ( $this->getTrace() as $i => $ex ) { 
				$args = '';
				foreach ($ex['args'] as $item){
					$args.= ",'".$item."'";
				}
				$html.= '<li>';
				$html.= $ex['file'].'(<span class="error_line">'.$ex['line'].'</span>): '."\n";
				$html.= '<span class="error_content">'.$ex['class'].$ex['type'].$ex['function'].'('.substr($args,1).')'.'</span>';
				$html.= '</li>';
			}
			$html.= '</ul></pre></div>';
			echo $html;
		} else {
			if (!is_null($this->status)) {
				
			}
		}
	}
	
	public function __toString()
    {
        return get_class($this) . " '{$this->message}' in {$this->file}({$this->line})\n"
                                . "{$this->getTraceAsString()}";
    }
	
	/***************************************************************************/
	function loadfile($handle){
		try {
			if (! file_exists( $handle )) {
				throw new Exception ( "Loadfile: $handle is not a valid handle." );
				return false;
			}
			$filename = $this->filename ( $handle );
			
			$str = implode ( "", @file ( $filename ) );
			if (empty ( $str )) {
				throw new Exception ( "Loadfile: While loading $handle, $filename does not exist or is empty." );
				return false;
			}
			return $str;
			
		} catch ( Exception $ex ) {
			echo nl2br($ex->getTraceAsString());
		}
	}
	/***************************************************************************/
	/** private: filename($filename)
   	 * filename: name to be completed.
   	 */
	function filename($filename) {
		try {
			if (substr ( $filename, 0, 1 ) != "/") {
				$filename = "./" . $filename;
			}
			
			if (! file_exists ( $filename )) {
				throw new Exception ( "filename: file $filename does not exist." );
				$this->halt ( "filename: file $filename does not exist." );
			
			}
			return $filename;
		} catch ( Exception $ex ) {
			echo nl2br($ex->getTraceAsString());
		}
	}
}

class SysExceptionModel extends SysException {
	function __construct($msg='') {
		if (IS_DEBUG && IS_DEBUG_DATABASE === true) {
			parent::__construct($msg,EXCEPTION_ERROR_SQL);
			echo 'Database is ERROR';
		} else {
			return 'SysExceptionModel';
		}
	}
	
}
class SysExceptionControl extends SysException {
	function __construct($msg='') {
		if (IS_DEBUG) {
			parent::__construct($msg,EXCEPTION_ERROR);
		} else {
			return 'SysExceptionControl';
		}
	}
	
}